package com.shengqi.xinnongren.work.ui.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.utils.DateTools;
import com.shengqi.xinnongren.work.ui.goods.model.CommentBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论适配器
 */
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.Holder> {

    private Context mContext;
    private List<CommentBean> mCommentBeanList;
    private RequestOptions mRequestOptions;

    public CommentsAdapter(Context context, List<CommentBean> commentBeanList) {
        mContext = context;
        mCommentBeanList = commentBeanList;
        mRequestOptions = new RequestOptions().placeholder(R.mipmap.img_place_holder);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_goods_comment, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {

        CommentBean commentBean = mCommentBeanList.get(position);
        Glide.with(mContext).load(commentBean.headUrl).apply(mRequestOptions).into(holder.ivAvatar);
        holder.tvNickName.setText(commentBean.nickName);
//        holder.tvDate.setText(DateTools.getTime(commentBean.getAddDatetime()));
        holder.tvDate.setText(commentBean.addTime);
        if (TextUtils.isEmpty(commentBean.content))
            holder.tvContent.setText("暂无评论");
        else
            holder.tvContent.setText(commentBean.content);
        holder.rbScore.setRating(commentBean.score);

        String replyStr = commentBean.getReply();
        if (!TextUtils.isEmpty(replyStr)) {
            holder.rlReplyContainer.setVisibility(View.VISIBLE);
            holder.tvReply.setText(Html.fromHtml("<font color='#fc9341'>商家回复：</font>" + replyStr));
        } else {
            holder.rlReplyContainer.setVisibility(View.GONE);
        }

        String picStr = commentBean.picUrl;
        if (!TextUtils.isEmpty(picStr)) {
            String[] pics = picStr.split(",");
            if (pics.length == 0) return;
            holder.rvPictures.setVisibility(View.VISIBLE);
            holder.rvPictures.setAdapter(new PicturesAdapter(mContext, PicturesAdapter.Type.GRID, pics));
            holder.rvPictures.setLayoutManager(new GridLayoutManager(mContext, 4));
        } else {
            holder.rvPictures.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mCommentBeanList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.rv_pictures)
        RecyclerView rvPictures;
        @BindView(R.id.iv_avatar)
        ImageView ivAvatar;
        @BindView(R.id.tv_nickname)
        TextView tvNickName;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.rb_score)
        RatingBar rbScore;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_reply)
        TextView tvReply;
        @BindView(R.id.rl_reply_container)
        RelativeLayout rlReplyContainer;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}